<template>
  <div class="goods">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/goods' }">商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <!-- .native修饰符的作用是，让封装的组件也能够绑定原声的事件 -->
    <el-input placeholder="请输入搜索内容" v-model="searchval" @keydown.enter.native='searchUser' class="search-input">
      <el-button slot="append" icon="el-icon-search" @click='searchUser'></el-button>
    </el-input>
    <el-button type="primary" plain>添加商品</el-button>
    <!-- 表格 -->
    <el-table :data="goodsList" class='mt-15' border style="width: 100%">
      <el-table-column prop="cat_id" type=index label="编号" width='50px'> </el-table-column>
      <el-table-column prop="goods_name" label="商品名称" width='350px'> </el-table-column>
      <el-table-column prop="goods_price" label="商品价格" width='80px'> </el-table-column>
      <el-table-column prop="is_promote" label="商品状态" width='80px'> </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量" width='100px'> </el-table-column>
      <el-table-column prop="add_time" label="创建时间" width='130px'></el-table-column>
      <el-table-column prop="is_promote" label="操作">
        <template slot-scope='scope'>
          <el-button type="primary" icon="el-icon-edit" circle></el-button>
          <el-button type="danger" icon="el-icon-delete" circle></el-button>
          <el-button type="warning" icon="el-icon-star-off" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页功能 -->
    <el-pagination
      class='page mt-15'
      :pager-count="5"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="1"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="goodstotal">
    </el-pagination>
  </div>
</template>
<script>
import { getGoodsList } from '@/api'
export default {
  data () {
    return {
      searchval: '',
      goodsList: [],
      goodstotal: 0, // 用户总条数
      pagesize: 10, // 每页显示多少条，默认10条
      goodsnum: 1 // 当前页码，默认为第一页
    }
  },
  created () {
    this.initGoodsList()
  },
  methods: {
    initGoodsList () {
      getGoodsList({ query: this.searchval, pagenum: this.goodsnum, pagesize: this.pagesize }).then(res => {
        // console.log(res)
        this.goodstotal = res.data.total
        this.goodsList = res.data.goods
      })
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      // 改变每页多少条时，将这个值赋值给pagesize属性，再调用获取数据的方法
      this.pagesize = val
      this.initGoodsList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.goodsnum = val
      this.initGoodsList()
    },
    searchUser () {
      console.log(234)
      this.initGoodsList()
    }
  }
}
</script>
<style lang="scss" scoped>
.search-input{
  width: 300px;
  margin-right: 5px;
}
</style>
